package tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

import toolkit.CollectionUtils;
import toolkit.Debug;
import bean.PizzaBean;
import domain.value.Ingredient;

public class DailyPizzaTag extends AbstractTag
{
	private Tag tag = null;
	
	private PageContext pageContext = null;
	
	private List<PizzaBean> pizzas = null;
	
	public DailyPizzaTag()
	{
		Debug.log("(" + id + ") DailyPizzaTag()");
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException
	{
		Debug.log("(" + id + ") DailyPizzaTag.doStartTag()");
		
		String body = "";
		
		if (pizzas != null && pizzas.size() > 0)
		{
			// Do the suffle
			Debug.log("------------------------------------------");
			Debug.log(pizzas);
			java.util.Collections.shuffle(pizzas);
			Debug.log(pizzas);
			
			int dayOfWeek = new GregorianCalendar().get(Calendar.DAY_OF_WEEK);
			
			for (PizzaBean bean: pizzas)
			{
				try
				{
					String name = (String) bean.get("name");
					
					ArrayList<Ingredient> ingredients = (ArrayList<Ingredient>) bean.get("ingredients");
					
					String price = (String) bean.get("price");
					
					int discountDay = Integer.parseInt((String) bean.get("discountDay"));
					
					if(dayOfWeek != discountDay)
					{
						continue;
					}
					
					String ingrString;
					
					if (ingredients != null && ingredients.size() > 0)
					{
						ingrString = CollectionUtils.join(ingredients, ", ");;
					}
					else
					{
						ingrString = "Onbekend";
					}
					
					body += ("<strong>" + name + " &euro; " + price + "</strong>" +
							"<p>Ingredi&euml;nten: " + ingrString + "</p>");
					
					break;
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		}
		
		if(body.equals(""))
		{
			body += "Er is vandaag geen pizza van de dag.";
		}

		try
		{
			pageContext.getOut().print(body);
		}
		catch (IOException e)
		{
			Debug.log(e);
		}
		
		return EVAL_PAGE;
	}
	
	@Override
	public int doEndTag() throws JspException
	{
		Debug.log("(" + id + ") DailyPizzaTag.doEndTag()");
		
		return SKIP_BODY;
	}
	
	@Override
	public void setParent(Tag tag)
	{
		Debug.log("(" + id + ") DailyPizzaTag.setParent(" + tag + ")");
		
		this.tag = tag;
	}
	
	@Override
	public Tag getParent()
	{
		Debug.log("(" + id + ") DailyPizzaTag.getParent()");
		
		return tag;
	}
	
	@Override
	public void setPageContext(PageContext pageContect)
	{
		Debug.log("(" + id + ") DailyPizzaTag.setPageContext(" + pageContect + ")");
		
		pageContext = pageContect;
	}
	
	public void setBeans(List<PizzaBean> beans)
	{
		Debug.log("(" + id + ") DailyPizzaTag.setBeans(" + beans + ")");
		
		if(beans == null)
		{
			return;
		}
		
		pizzas = new ArrayList<PizzaBean>(beans);
	}
	
	@Override
	public void release()
	{
		Debug.log("(" + id + ") DailyPizzaTag.release()");
	}
	
	@Override
	protected void finalize() throws Throwable
	{
		Debug.log("(" + id + ") DailyPizzaTag.finalize()");
	}
}
